<template>
  <!--授权pdi站点	-->
  <div>
    <FilterTable
      height="auto"
      ref="tableInfo"
      :columns="columns"
      :isSelectColumn="isSelectColumn"
      selectionType="single"
      :isMemoryPagination="true"
      :onSyncTableData="syncFun"
      :datakey="'records'"
      :isToperInfo="false"
      :onColumnsChange="columns => (this.columns = columns)"
      columnsRef="notification_maintain_info_02">

      <template slot="controls">
        <el-button v-show="formType == 'add' || formType == 'edit'" size="small" type="primary" icon="el-icon-plus" @click="addFun()">新增</el-button>
        <BaseDialog :visible.sync="isSearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
          <DealerSearcher :rowData="upperData" @close="upperClose" />
        </BaseDialog>
      </template>
    </FilterTable>
  </div>
</template>

<script>
  import { dictionary } from '@/utils/dictMixin';
  import * as types from '@/api/cms-api/origin/originInfo/types';
  import DealerSearcher from '@/pages/cms-pages/origin/originInfo/serviceDealerForm/DealerSearcher.vue';

  import vm from './bridge';

  export default {
    mixins: [dictionary],
    model: {
      prop: 'pdata',
      event: 'click'
    },
    props: {
      pdata: {
        type: Object,
        default: () => ({})
      },
      formType: {
        type: String,
        required: true
      },
      formOptions: {
        type: Object,
        required: true
      }
    },
    components: {
      DealerSearcher
    },
    data() {
      return {
        columns: this.createColumns(),
        isSelectColumn: false,
        saveData: {
          editType: '',
          xxList: [],
        },
        upperData: {},
        isSearch: false,
      };
    },
    mounted() {
      //注册经销商类型变化时触发的事件
    },
    methods: {
      //初始化数据
      init(data) {
        let pdataCopy = JSON.parse(JSON.stringify(this.pdata));
        this.$refs.tableInfo.SET_TABLE_DATA(pdataCopy.attachRelatedDealerList);
      },
      //表单验证通过后
      formChangeHandle(val) {
        this.pdata = Object.assign(this.pdata, val);
        //传递数据到父组件里
        this.$emit('click', this.pdata);
      },
      //开放的提交方法
      saveDialog() {
        this.$refs.FormPanel.SUBMIT_FORM();
      },

      createColumns(){
        return [
          {
            title: '操作',
            dataIndex: 'column-action',
            width: 50,
            fixed: 'left',
            render: (props, h) => {
              return (
                <div>
                <el-button v-show={this.formType == 'add' || this.formType == 'edit'} size="mini" type="text" onClick={() => this.deleteRow(props.row,props.$index)}>
              删除
              </el-button>
              </div>
            );
            }
          },
          {
            title: '顺序',
            dataIndex: 'index',
            width: 80,
            sorter: true,
            render: props => {
              return <span>{props.$index + 1}</span>;
            }
          },
          {
            title: 'dealerAttachId',
            dataIndex: 'dealerAttachId',
            filterType: 'input',
            sorter: true,
            editable: true,
            editRequired: true,
            placeholder: '',
            hidden: true,
          },
          {
            title: '经销商代码',
            dataIndex: 'dealerCode',
            filterType: 'input',
            sorter: true,
            filter: true,
            editable:false,
          },
          {
            title: '经销商名称',
            dataIndex: 'dealerName',
            filterType: 'input',
            sorter: true,
            editable: false,
          },
          {
            title: '经销商简称',
            dataIndex: 'dealerShortname',
            filterType: 'input',
            sorter: true,
            editable: false,
          },
          {
            title: '经销商地址',
            dataIndex: 'salesAddress',
            filterType: 'input',
            sorter: true,
            editable: false,
          },
        ];
      },

      deleteRow(row,index) {
        this.$confirm('确认删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.$refs.tableInfo.EXECUTE_DELETE([row])
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            });
          });
      },

      syncFun(details){
        this.saveData.xxList = details
      },

      addFun(){
        this.isSearch = true;
        // this.$refs.tableInfo.EXECUTE_INSERT(p)
      },

      upperClose(val, data) {
        if(val != 2){
          this.isSearch = false;
        }else{
          if(data){
            let flag = true;
            this.saveData.xxList.forEach(t => {
              if(t.dealerAttachId == data[0].dealerAttachId){
                this.$notify.warning("请勿重复添加");
                flag = false;
                return;
              }
            });
            if(!flag){
              return;
            }
            this.$refs.tableInfo.EXECUTE_INSERT(data);
          }
          this.isSearch = false;
        }
      },

    }
  };
</script>
